package com._520it.wms.web.controller;

import com._520it.wms.domain.SystemMenu;
import com._520it.wms.query.SystemMenuQueryObject;
import com._520it.wms.service.ISystemMenuService;
import com._520it.wms.util.JSONResult;
import com._520it.wms.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Created by HASEE on 2017/11/14.
 */
@Controller
@RequestMapping("/systemMenu")
public class SystemMenuContorller {

    @Autowired
    private ISystemMenuService systemMenuService;

    @RequestMapping("/list")
    @RequiredPermission("系统菜单列表")
    public String list(Model model, @ModelAttribute("qo") SystemMenuQueryObject qo) {
        model.addAttribute("pageResult", systemMenuService.query(qo));
        List<Map<String, Object>> parentList = systemMenuService.queryParentList(qo.getParentId());
        model.addAttribute("parentList", parentList);
        return "/systemMenu/list";
    }

    @RequestMapping("/input")
    @RequiredPermission("系统菜单编辑")
    public String input(Long id, Model model, Long parentId) {
        if (parentId != null) {
            SystemMenu parent = systemMenuService.get(parentId);
            model.addAttribute("parent", parent);
        }

        if (id != null) {
            SystemMenu systemMenu = systemMenuService.get(id);
            model.addAttribute("systemMenu", systemMenu);
        }
        return "/systemMenu/input";
    }

    @RequestMapping("delete")
    @RequiredPermission("系统菜单删除")
    @ResponseBody
    public JSONResult delete(Long id) {
        JSONResult jsonResult = new JSONResult();
        try {
            if (id != null) {
                systemMenuService.delete(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission("系统菜单新增/更新")
    @ResponseBody
    public JSONResult saveOrUpdate(SystemMenu systemMenu) {
        systemMenuService.saveOrUpdate(systemMenu);
        return new JSONResult();

    }

    @RequestMapping("/queryMenusByParentSn")
    @ResponseBody
    public List<Map<String, Object>> queryMenusByParentSn(SystemMenuQueryObject qo) {
        List<Map<String, Object>> list = systemMenuService.queryMenusByParentSn(qo.getParentId());
        return list;
    }
}
